package bancosys.tec.persist;

import java.util.concurrent.Callable;

import bancosys.tec.persist.authorization.AuthorizationManager;
import bancosys.tec.persist.authorization.AuthorizationStatus;
import bancosys.tec.persist.controller.TransactionalController;

/**
 * Controller transacional.
 * 
 * @author Cesar Olmos
 */
public class PersistenceController extends TransactionalController {

    private AuthorizationManager authorizationManager;

    /**
     * Executa a ação com a autorização desligada.
     * 
     * @param action a ação.
     * @param <V> o tipo do resultado da ação.
     * @return o resultado da ação.
     * @throws Exception caso ocorra algum erro durante a execução da ação.
     */
    // XXX (lundberg) esse tipo de solução ainda deve ser debatida/decidida.
    public <V> V executeWithDisabledAuthorization(Callable<V> action) throws Exception {
        AuthorizationStatus previousStatus = this.authorizationManager.getThreadAuthorizationStatus();
        try {
            this.authorizationManager.setThreadAuthorizationStatus(AuthorizationStatus.DISABLED);
            return action.call();
        } finally {
            this.authorizationManager.setThreadAuthorizationStatus(previousStatus);
        }
    }

    /**
     * @return the authorizationManager
     */
    public AuthorizationManager getAuthorizationManager() {
        return this.authorizationManager;
    }

    /**
     * @param authorizationManager the authorizationManager to set
     */
    public void setAuthorizationManager(AuthorizationManager authorizationManager) {
        this.authorizationManager = authorizationManager;
    }
}
